package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.place;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class PlaceBusinessConfigController {

    @Route(value = "/place/business_config/list.data")
    public ListResult<PlaceBusinessConfig> listData(HttpServletRequest request) {
        ListResult<PlaceBusinessConfig> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long placeId = WebUtils.getLong(request,"placeId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore physicalStore = PlacePhysicalStoreService.self().getById(placeId);
            if(physicalStore != null && physicalStore.getUserId().equals(loginUser.getId())) {
                List<PlaceBusinessConfig> placeDeviceFuncList = PlaceBusinessConfigService.self().findByPlaceId(placeId);
                if (placeDeviceFuncList != null && placeDeviceFuncList.size() > 0) {
                    result.setCode(DtoConstants.SUCCESS, null);
                    result.getBody().setData(placeDeviceFuncList);
                }
            }
        }
        return result;
    }

    @Route(value = {"/place/business_config/add.html"})
    public String add(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request, "placeId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore physicalStore = PlacePhysicalStoreService.self().getById(placeId);
            if (physicalStore != null && physicalStore.getUserId().equals(loginUser.getId())) {
                request.setAttribute("place", physicalStore);
            }
        }
        List<EBusinessType> businessTypes = new ArrayList<>();
        businessTypes.add(EBusinessType.CDCZ);
        businessTypes.add(EBusinessType.CDB);
        businessTypes.add(EBusinessType.BXSCN);
        businessTypes.add(EBusinessType.BXSCN_SHOP);
        request.setAttribute("businessTypeList",businessTypes);
        return "/place/business_config/edit.html";
    }

    @Route(value = {"/place/business_config/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        Long placeId = WebUtils.getLong(request, "placeId");
        String businessType = WebUtils.getString(request,"businessType");
        if(placeId != null && StringTools.isNotBlank(businessType)){
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                PlacePhysicalStore physicalStore = PlacePhysicalStoreService.self().getById(placeId);
                if (physicalStore != null && physicalStore.getUserId().equals(loginUser.getId())) {
                    PlaceBusinessConfig placeDeviceFunc = new PlaceBusinessConfig();
                    placeDeviceFunc.setPlaceId(placeId);
                    placeDeviceFunc.setBusinessType(businessType);

                    try {
                        if (PlaceBusinessConfigService.self().add(placeDeviceFunc)) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        result.setMsg(e.getMessage());
                    }
                }else {
                    result.setCode(DtoConstants.NOT_POWER,"无权限操作");
                }
            }
        }
        return result;
    }

    @Route(value = {"/place/business_config/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败，请稍后再试");
        Long placeId = WebUtils.getLong(request,"placeId");
        String businessType = WebUtils.getString(request,"businessType");
        if(placeId != null && StringTools.isNotBlank(businessType)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                PlacePhysicalStore physicalStore = PlacePhysicalStoreService.self().getById(placeId);
                if (physicalStore != null && physicalStore.getUserId().equals(loginUser.getId())) {
                    try {
                        if (PlaceBusinessConfigService.self().deleteByKeys(placeId, businessType)) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        result.setMsg(e.getMessage());
                    }
                }else {
                    result.setCode(DtoConstants.NOT_POWER,"无权限操作");
                }
            }
        }
        return result;
    }

    @Route(value = {"/place/business_config/cost.html"})
    public void funcCost(HttpServletRequest request, HttpServletResponse response) {
        Long placeId = WebUtils.getLong(request,"placeId");
        String businessType = WebUtils.getString(request,"businessType");
        if(EBusinessType.CDCZ.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/cdcz/cost/place/config.html?placeId="+placeId);
        }else if(EBusinessType.CDB.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/cdb/cost/place/config.html?placeId="+placeId);
        }else if(EBusinessType.BXSCN.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/bxscn/cost/place/config.html?placeId="+placeId);
        }else if(EBusinessType.BXSCN_SHOP.index().equals(businessType)){
            WebUtils.sendRedirect(request,response,"/bxscn/cost/place/shop_config.html?placeId="+placeId);
        }
    }

}
